package com.ctl.controller;

import com.ctl.client.NettyClient;
import com.ctl.config.MapBeanConfig;
import com.ctl.config.ServerConfig;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;

//import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
public class ClientController {
    @Autowired
    @Qualifier("serviceExecutor")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Autowired
    private NettyClient nettyClient;
    @Autowired
    private ServerConfig serverConfig;


    @RequestMapping("/start")
    @ResponseBody
    public Object send(HttpServletRequest request, Long times) {
        log.info(" nettyClient is start,number={}", times);
        for (int i = 0; i < times; i++) {
            //com.ctl.config.ExecutorConfig.maxPoolSize决定最多启动多少个client(多余的无法加入线程池,虽然策略为丢失旧的)
            threadPoolTaskExecutor.submit(() -> nettyClient.start());
            log.info("start client i={},threadPoolTaskExecutor.activeCount={}", i+1, threadPoolTaskExecutor.getActiveCount());
        }
        return times;
    }
}
